package com.efast.dmom.aps.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.aps.bean.ResultBean;
import com.efast.dmom.aps.bean.Shift;
import com.efast.dmom.aps.datahandlers.ShiftDataHandler;
import com.efast.dmom.aps.datahandlers.WorkingCalendarShiftDataHandler;
import com.efast.dmom.aps.service.ShiftService;
import com.efast.dmom.aps.util.StringVerifyUtil;

@Service
public class ShiftServiceImpl implements ShiftService {
	@Autowired
	private ShiftDataHandler shiftDataHandler;
	@Autowired
	private WorkingCalendarShiftDataHandler workingCalendarShiftDataHandler;
	
	@Override
	public ResultBean save(Shift shift) {
		ResultBean result = new ResultBean(true, "'");
		// 进行班次编号、班次名称、加班时间的必填校验//
		if (StringUtils.isNotBlank(shift.getInnnerData().getShiftCode())
				&& StringUtils.isNotBlank(shift.getInnnerData().getName()) && shift.getInnnerData().getOverTime() >= 0
				&& shift.getInnnerData().getEndTime() != null && shift.getInnnerData().getStartTime() != null) {
			// 开始、结束时间比较大小
			int compareTimeNumber = shift.getInnnerData().getEndTime()
					.compareTo(shift.getInnnerData().getStartTime());
			if (compareTimeNumber > 0) {
				shift.getInnnerData().setDaySpan(0);
			} else {
				shift.getInnnerData().setDaySpan(1);
			}
			// 进行加班时间不能大于总工作时长的校验
			long countCompare = timeCompare(shift.getInnnerData().getEndTime(), shift.getInnnerData().getStartTime());
			if (shift.getInnnerData().getOverTime() < countCompare) {
				// 对code进行唯一性校验
				Shift shiftResult = shiftDataHandler.getByShiftCode(shift.getInnnerData().getShiftCode());
				if (shiftResult.getInnnerData() != null) {
					result.setSuccess(false);
					result.setMessage("班次编号不唯一，请重填！");
					return result;
				}
				if (!StringVerifyUtil.isstring(shift.getInnnerData().getShiftCode())) {
					  result.setSuccess(false);
					  result.setMessage("客户编码不是由英文、字母、下划线组成!!");
					  return result;
				  }
				Shift shiftResult2 = shiftDataHandler.getByName(shift.getInnnerData().getName());
				if (shiftResult2.getInnnerData() != null) {
					result.setSuccess(false);
					result.setMessage("班次名称不唯一，请重填！");
					return result;
				}
				shiftDataHandler.save(shift);
				result.setSuccess(true);
				result.setMessage("新增成功！");
			} else {
				result.setSuccess(false);
				result.setMessage("加班时间不应大于班次总时间，请修改");
				return result;
			}
		} else {
			result.setSuccess(false);
			result.setMessage("请检查班次编号、班次名称、开始结束时间是否填写！");
			return result;
		}
		return result;
	}

	@Override
	public ResultBean update(Shift shift) {
		ResultBean result = new ResultBean(true, "");
		// 进行班次编号、班次名称、加班时间的必填校验//
		if (StringUtils.isNotBlank(shift.getInnnerData().getShiftCode()) && StringUtils.isNotBlank(shift.getInnnerData().getName())
				&& shift.getInnnerData().getOverTime() >= 0) {
			// 开始、结束时间比较大小
						int compareTimeNumber = shift.getInnnerData().getEndTime()
								.compareTo(shift.getInnnerData().getStartTime());
						if (compareTimeNumber > 0) {
							shift.getInnnerData().setDaySpan(0);
						} else {
							shift.getInnnerData().setDaySpan(1);
						}
			Shift shiftById = shiftDataHandler.getById(shift.getInnnerData().getId());		
			if ((shift.getInnnerData().getShiftCode()).equals(shiftById.getInnnerData().getShiftCode())) {
				shiftDataHandler.update(shift);
				result.setSuccess(true);
				result.setMessage("修改成功！");
			} else {
				result.setSuccess(false);
				result.setMessage("班次编号不能修改！");
				return result;
			}
		} else {
			result.setSuccess(false);
			result.setMessage("请检查班次编号、班次名称、加班时间是否填写！");
			return result;
		}
		return result;
	}

	@Override
	public ResultBean delete(Shift shift) {
		// 判断班次详情是否引用了班次信息，在进行删除校验
		int countRest = workingCalendarShiftDataHandler.getCountByShiftId(shift.getInnnerData().getId());
		ResultBean result = new ResultBean(true, "'");
		if (countRest == 0) {
			// 逻辑删除
			shiftDataHandler.delete(shift);
			result.setSuccess(true);
			result.setMessage("删除成功！");
		} else {
			result.setSuccess(false);
			result.setMessage("该条数据已被工作日历引用，无法进行删除！");
			return result;
		}
		return result;
	}




	// 比较开始时间与结束时间大小判断
	public long timeCompare(LocalTime localTime1, LocalTime localTime2) {
		// SimpleDateFormat dateFormat = new SimpleDateFormat("HH-mm");
		// String Time1=dateFormat.format(localTime1);
		// String Time2=dateFormat.format(localTime2);
		int hour1 = localTime1.getHour();
		int minute1 = localTime1.getMinute();
		int hour2 = localTime2.getHour();
		int minute2 = localTime2.getMinute();
		String time1 = hour1 + ":" + minute1;
		String time2 = hour2 + ":" + minute2;

		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		Date date1 = null;
		Date date2 = null;
		long minutes = 0;
		try {
			date1 = format.parse(time1);
			date2 = format.parse(time2);
			long countTime = date1.getTime() - date2.getTime();
			minutes = countTime / (1000 * 60);
			if (minutes < 0) {
				minutes = minutes + 1440;
			}
		} catch (ParseException e) {
			e.printStackTrace();

		}
		return minutes;

	}

	@Override
	public Shift getShiftByShiftName(String shiftName) {
		return shiftDataHandler.getByName(shiftName);
	}

	@Override
	public List<Shift> getAll() {
		return shiftDataHandler.getAll();
	}

	@Override
	public Shift getShiftByShiftId(Long shiftId) {
		return shiftDataHandler.getById(shiftId);
	}

	@Override
	public Shift getShiftByShiftCode(String shiftCode) {
		return shiftDataHandler.getByShiftCode(shiftCode);
	}

}
